home *** CD-ROM | disk | FTP | other *** search
/ CGI How-To / CGI HOW-TO.iso / chap7 / 7_1 / ht71.c next >
Encoding:
C/C++ Source or Header  |  1996-06-15  |  3.1 KB  |  120 lines

  1. /*
  2.  * Compile with:
  3.  *     cc string.o array.o dict.o cgilib.o ht71.c -o ht71.cgi
  4.  */
  5.  
  6. #include <stdio.h>
  7. #include "cgilib.h"
  8.  
  9. /*
  10.  * Configurable Constants
  11.  *     The default values may be overwriten from the C
  12.  *     compiler's command line.
  13.  *     Ex. on a UN*X system
  14.  *     cc -DSUBJECT="\"Poll Results\"" \
  15.  *         string.o array.o cict.o cgilib.o ht71.c -o ht71.cgi
  16.  */
  17. #ifndef SENDMAIL
  18. #    define SENDMAIL "/usr/bin/sendmail"
  19. #endif
  20. #ifndef TO
  21. #    define TO       "kgr"
  22. #endif
  23. #ifndef SUBJECT
  24. #    define SUBJECT  "Survey Results"
  25. #endif
  26. #ifndef BACK
  27. #    define BACK     "<A HREF=\"/kgr/book\">Back to my Homepage</A>"
  28. #endif
  29.  
  30. /*
  31.  * It is expected that values for the following field names will
  32.  * be supplied via the CGI interface.  The list is terminated
  33.  * with a NULL.
  34.  */
  35. static char *names[] = {
  36.     "name", "email", "street", "city",
  37.     "state", "zip",  "rating", "comments",
  38.     NULL 
  39. };
  40.  
  41.  
  42. void main(int argc, char *argv[])
  43. {
  44.     FILE *mail;                       /* pipe to stdin of sendmail */
  45.     Dictionary dataDict = readParse();/* CGI parameters            */
  46.     int i;                            /* index into *names[]       */
  47.     char buf[100];                    /* sendmail commandline buf  */
  48.     char *email;                      /* from email address        */
  49.     char *name;                       /* senders real name         */
  50.    
  51.     /* Output the HTML content type */
  52.     printf("Content-type: text/html\n\n");
  53.  
  54.     /* The -t causes the To: field to be read from standard
  55.      * input instead of being expected on the command line.
  56.      * The -oi prevents a dot on a line by itself from being
  57.      * interpreted as a message terminator.
  58.      */
  59.     sprintf(buf,"%s -t -oi",SENDMAIL);
  60.     mail = popen(buf,"w");
  61.  
  62.     email = dict_isKey(dataDict,"email") ?
  63.         dict_valueForKey(dataDict,"email") : "";
  64.  
  65.     name = dict_isKey(dataDict,"name") ?
  66.         dict_valueForKey(dataDict,"name") : "";
  67.  
  68.     /*
  69.      * Output the mail header 
  70.      */
  71.     fprintf(mail, "Reply-to: %s, (%s)\n", email, name);
  72.     fprintf(mail, "From: %s, (%s)\n", email, name); 
  73.     fprintf(mail, "To: %s\n", TO);
  74.     fprintf(mail, "Subject: %s\n\n", SUBJECT);
  75.     
  76.     /*
  77.      * Output the mail body 
  78.      */
  79.     for ( i = 0 ; names[i] ; i++ )
  80.     {
  81.         fprintf(mail,"<%s>\n",names[i]);
  82.         if (dict_isKey(dataDict,names[i]))
  83.         {
  84.             fprintf(mail,"%s\n",
  85.             dict_valueForKey(dataDict,names[i]));
  86.     }
  87.         fprintf(mail,"\n");
  88.     }
  89.    
  90.    
  91.     /*
  92.      * Output the mail footer
  93.      */
  94.     fprintf(mail, "\n<REMOTE HOST>\n%s\n\n",
  95.         getenv("REMOTE_HOST"));
  96.    
  97.     fprintf(mail, "\n<REMOTE ADDR>\n%s\n\n",
  98.         getenv("REMOTE_ADDR"));
  99.    
  100.     fprintf(mail, "\n<USER AGENT>\n%s\n\n",
  101.         getenv("HTTP_USER_AGENT"));
  102.    
  103.     /* Close the pipe, sending the mail */
  104.     pclose(mail);
  105.    
  106.     /*
  107.      * Generate HTML notification
  108.      */
  109.    
  110.     printf("<HTML><BODY><H1>Your comments have been noted.<BR>");
  111.     printf("Thank you for your time.<BR><BR>%s</H1>",BACK);
  112.     printf("</BODY></HTML>");
  113.  
  114.     /* Free the dynamic memory associated with dataDict. */
  115.     dict_free(dataDict);
  116.    
  117.     /* End the program */
  118.     exit(0);
  119. }
  120.